package pers.xiaojun.boot.module.system.controller.permission;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.common.util.BeanUtils;
import pers.xiaojun.boot.module.system.controller.permission.vo.menu.MenuReqVO;
import pers.xiaojun.boot.module.system.controller.permission.vo.menu.MenuRespVO;
import pers.xiaojun.boot.module.system.controller.permission.vo.menu.MenuSaveVO;
import pers.xiaojun.boot.module.system.controller.permission.vo.menu.MenuSimpleRespVO;
import pers.xiaojun.boot.module.system.dao.entity.permission.MenuDO;
import pers.xiaojun.boot.module.system.service.permission.MenuService;
import pers.xiaojun.boot.web.core.annotation.AdminApi;

import java.util.Comparator;
import java.util.List;

/**
 * 菜单窗口
 *
 * @author xiaojun
 * @since 2025-10-25
 */
@Tag(name = "菜单")
@AdminApi
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/menu")
public class MenuController {

    private final MenuService menuService;

    @PostMapping()
    @Operation(summary = "创建菜单")
    @PreAuthorize("@ss.hasPermission('system:menu:create')")
    public CommonResult<?> createMenu(@RequestBody @Valid MenuSaveVO createVO) {
        MenuDO menu = menuService.saveMenu(createVO);
        return CommonResult.success(menu.getId());
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新菜单")
    @PreAuthorize("@ss.hasPermission('system:menu:update')")
    @Parameter(name = "id", description = "菜单Id", required = true, example = "1")
    public CommonResult<?> updateMenu(@PathVariable("id") Long menuId, @RequestBody @Valid MenuSaveVO updateVO) {
        updateVO.setId(menuId);
        MenuDO menu = menuService.updateMenu(updateVO);
        return CommonResult.success(menu.getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除菜单")
    @PreAuthorize("@ss.hasPermission('system:menu:delete')")
    @Parameter(name = "id", description = "菜单Id", required = true, example = "1")
    public CommonResult<?> deleteMenu(@PathVariable("id") Long menuId) {
        menuService.removeMenuById(menuId);
        return CommonResult.success();
    }


    @DeleteMapping("/batch/{ids}")
    @Operation(summary = "批量删除菜单")
    @PreAuthorize("@ss.hasPermission('system:menu:delete')")
    @Parameter(name = "ids", description = "菜单Id数组（使用 , 分隔）", required = true, example = "1,2,3")
    public CommonResult<?> deleteMenu(@PathVariable("ids") List<Long> menuIds) {
        menuService.removeMenuByIds(menuIds);
        return CommonResult.success();
    }


    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    @Operation(summary = "获取菜单信息")
    @Parameter(name = "id", description = "菜单Id", required = true, example = "1")
    public CommonResult<MenuRespVO> getMenu(@PathVariable("id") Long menuId) {
        return CommonResult.success(menuService.getMenuById(menuId));
    }

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    @Operation(summary = "获取菜单列表", description = "获取菜单列表，可条件查询")
    public CommonResult<List<MenuRespVO>> getMenuList(MenuReqVO menuReqVO) {
        List<MenuDO> menus = menuService.listMenus(menuReqVO);
        menus.sort(Comparator.comparing(MenuDO::getSort));
        return CommonResult.success(BeanUtils.toBeans(menus, MenuRespVO.class));
    }

    @GetMapping("/simple-list")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    @Operation(summary = "获取精简菜单列表")
    public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuList() {
        List<MenuDO> menus = menuService.list();
        menus.sort(Comparator.comparing(MenuDO::getSort));
        return CommonResult.success(BeanUtils.toBeans(menus, MenuSimpleRespVO.class));
    }

    @GetMapping("/simple-list/tenant")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    @Operation(summary = "获取租户下的精简菜单列表")
    public CommonResult<List<MenuSimpleRespVO>> getSimpleMenuListByTenant() {
        List<MenuDO> menus = menuService.listMenusByTenant();
        return CommonResult.success(BeanUtils.toBeans(menus, MenuSimpleRespVO.class));
    }

}
